package ar.edu.unlam.tpfinal.tests.persistence;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import ar.edu.unlam.tpfinal.model.Priority;
import ar.edu.unlam.tpfinal.model.TransactionStatus;
import ar.edu.unlam.tpfinal.service.ClientService;
import ar.edu.unlam.tpfinal.service.TransactionService;
import ar.edu.unlam.tpfinal.service.TransactionTypeService;
import ar.edu.unlam.tpfinal.struts.forms.ClientForm;
import ar.edu.unlam.tpfinal.struts.forms.TransactionForm;
import ar.edu.unlam.tpfinal.struts.forms.TransactionTypeForm;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:TestClassService-context.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
public class TestClassService {

	@Autowired
	private ClientService clientService;
	
	@Autowired
	private TransactionService transactrionService;
	
	@Autowired
	private TransactionTypeService transactionTypeService;
	
	@Test
	@Transactional
	public void sampleTest() throws ParseException {
		List<ClientForm> clientFormList = clientService.getAllClients();
		
		Assert.assertTrue(clientFormList.size()==2);
		
		ClientForm clientForm = new ClientForm();
		clientForm.setFirstName("SebaShtian");
		clientForm.setLastName("PanonThin");
		List<Long> addressOidList = new ArrayList<Long>();
		addressOidList.add(new Long(1L));
		clientForm.setAddressOidList(addressOidList);
		clientForm.setEmail("panonsheba@hotmail.com");
		clientForm.setTelephone("45343-34342");
		clientService.insertClient(clientForm);
		
		clientFormList = clientService.getAllClients();
		
		Assert.assertTrue(clientFormList.size()==3);
		
		List<TransactionTypeForm>  transactionTypeFormList = transactionTypeService.listTransactionType();
		
		Assert.assertTrue(transactionTypeFormList.size()==1);
		
		TransactionTypeForm transactionTypeForm = new TransactionTypeForm();
		transactionTypeForm.setName("Venta Casa");
		transactionTypeForm.setComments("Venta de caasas de todo tipo");
		transactionTypeForm.setRequirements("Muchos papeles");
		transactionTypeService.saveTransactionType(transactionTypeForm);
		
		transactionTypeFormList = transactionTypeService.listTransactionType();
		
		Assert.assertTrue(transactionTypeFormList.size()==2);
		
		List<TransactionForm> transactionList = transactrionService.listTransaction();
		
		Assert.assertTrue(transactionList.size()==1);
		/*
		TransactionForm transactionForm = new TransactionForm();
		List<Long> clientListOid = new ArrayList<Long>();
		clientListOid.add(clientForm.getOid());
		transactionForm.setTransactionTypeOid(transactionTypeForm.getOid());
		transactionForm.setClients(clientListOid);
		Date date = new Date();
		transactionForm.setFromDate(date.getDay() + "-" + date.getMonth() + "-" + date.getYear());
		transactionForm.setToDate(date.getDay() + "-" + date.getMonth() + "-" + date.getYear());
		transactionForm.setComments("Venta de una casa");
		transactionForm.setPriority(Priority.LOW.name());
		transactionForm.setTransactionStatus(TransactionStatus.STARTED.name());
		transactrionService.saveTransaction(transactionForm);
		
		transactionList = transactrionService.listTransaction();
		
		Assert.assertTrue(transactionList.size()==2);
		*/
	}
}
